import { ComponentInfoType, ComponentsStateType } from '@/store/componentsReducer'

/**
 *  插入新组件
 */
export const insertNewComponent = (current: ComponentsStateType, newComponent: ComponentInfoType) => {
  const index = current.componentList.findIndex(item => item.fe_id === current.selectedId)
  if (index === -1) {
    // 没有选中组件，直接添加到最后
    current.componentList.push(newComponent)
  } else {
    // 有选中组件，添加到选中组件的后面
    current.componentList.splice(index + 1, 0, newComponent)
  }
  // 添加完后，选中新添加的组件
  current.selectedId = newComponent.fe_id || ''
}
